package com.xidian.zhiancloud.controller.myyb;

import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.SysAnnouncement;
import com.xidian.zhiancloud.service.myyb.IAnnounService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统设置-系统公告
 */
@RestController
@RequestMapping("/announcement")
public class AnnounController {

    @Autowired
    private IAnnounService announcementService;

    /**
     * 显示
     * @param title 标题
     * @param current 分页偏移量
     * @param size 每一页的数量
     * @return 返回 ResultDTO 对象
     */
    @GetMapping("/announcement_list")
    public ResultDTO<List<SysAnnouncement>> selectAnnouncementList(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "current", required = true) int current,
            @RequestParam(value = "size", required = true) int size) {
        return ResultDTO.successOf(announcementService.getList(title,current, size), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除
     * @param id 相关公告ID
     * @return 返回ResultDTO对象
     */
    @DeleteMapping("/deleteAnnouncement")
    public ResultDTO<Boolean> deleteAnnouncement(@RequestParam("id") Long id) {
        announcementService.removeById(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 查看
     *
     * @param id 相关公告ID
     * @return 返回ResultDTO对象
     */
    @GetMapping("/updateAnnouncement")
    public ResultDTO<SysAnnouncement> updateAnnouncement(@RequestParam("id") Long id) {
        return ResultDTO.successOf(announcementService.getById(id),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加
     *
     * @param announcement 相关公告对象
     * @return ResultDTO对象
     */ 
    @PostMapping("/add")
    public ResultDTO<Boolean> add(SysAnnouncement announcement) {
        announcementService.save(announcement);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 批量删除
     * @param ids 相关公告ids数组
     * @return 返回ResultDTO对象
     */
    @DeleteMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        announcementService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

}
